<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Jenkins," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="安装JDK&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;因为jenkins需要java环境，需要提前安装JDK。 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;下载JDK后解压 12[root@localhost ~]# tar zxvf jdk-8u111-linux-x64.tar.gz[roo">
<meta name="keywords" content="Jenkins">
<meta property="og:type" content="article">
<meta property="og:title" content="使用jenkins进行持续集成">
<meta property="og:url" content="https://hcldirgit.github.io/2017/09/19/jenkins/2.使用jenkins进行持续集成/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="安装JDK&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;因为jenkins需要java环境，需要提前安装JDK。 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;下载JDK后解压 12[root@localhost ~]# tar zxvf jdk-8u111-linux-x64.tar.gz[roo">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/01.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/02.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/03.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/04.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/05.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/06.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/07.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/08.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/09.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/10.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/11.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/12.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/13.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/14.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/15.png?raw=true">
<meta property="og:updated_time" content="2017-09-18T16:33:16.316Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="使用jenkins进行持续集成">
<meta name="twitter:description" content="安装JDK&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;因为jenkins需要java环境，需要提前安装JDK。 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;下载JDK后解压 12[root@localhost ~]# tar zxvf jdk-8u111-linux-x64.tar.gz[roo">
<meta name="twitter:image" content="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/01.png?raw=true">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/09/19/jenkins/2.使用jenkins进行持续集成/"/>





  <title>使用jenkins进行持续集成 | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/09/19/jenkins/2.使用jenkins进行持续集成/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">使用jenkins进行持续集成</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-19T00:10:09+08:00">
                2017-09-19
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="安装JDK"><a href="#安装JDK" class="headerlink" title="安装JDK"></a>安装JDK</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为jenkins需要java环境，需要提前安装JDK。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载JDK后解压</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># tar zxvf jdk-8u111-linux-x64.tar.gz</span></div><div class="line">[root@localhost ~]<span class="comment"># mv jdk1.8.0_111/ /usr/local/</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># vim /etc/profile.d/java.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">JAVA_HOME=/usr/<span class="built_in">local</span>/jdk1.8.0_111/</div><div class="line">JAVA_BIN=/usr/<span class="built_in">local</span>/jdk1.8.0_111/bin</div><div class="line">JRE_HOME=/usr/<span class="built_in">local</span>/jdk1.8.0_111/jre</div><div class="line">PATH=<span class="variable">$PATH</span>:/usr/<span class="built_in">local</span>/jdk1.8.0_111/bin:/usr/<span class="built_in">local</span>/jdk1.8.0_111/jre/bin</div><div class="line">CLASSPATH=/usr/<span class="built_in">local</span>/jdk1.8.0_111/jre/lib:/usr/<span class="built_in">local</span>/jdk1.8.0_111/lib:/usr/<span class="built_in">local</span>/jdk1.8.0_111/jre/lib/charsets.jar</div><div class="line"><span class="built_in">export</span> JAVA_HOME JAVA_BIN JRE_HOME PATH CLASSPATH</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后进入初始化环境变量</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># source /etc/profile.d/java.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果JDK配置成功，下面的名利个可以看到java版本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># java -version</span></div><div class="line">java version <span class="string">"1.8.0_111"</span></div><div class="line">Java(TM) SE Runtime Environment (build 1.8.0_111-b14)</div><div class="line">Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)</div></pre></td></tr></table></figure>
<h2 id="下载安装jenkins"><a href="#下载安装jenkins" class="headerlink" title="下载安装jenkins"></a>下载安装jenkins</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，从<a href="https://jenkins.io" target="_blank" rel="external">jenkins官方网站</a>下载最新的包。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;选择Red Hat/CentOS，可以根据官网提示安装</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/01.png?raw=true" alt="01"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo</div><div class="line">rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key</div><div class="line">yum install jenkins</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以下载相应的rpm包进行安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># rpm -ivh jenkins-2.78-1.1.noarch.rpm </span></div><div class="line">警告：jenkins-2.78-1.1.noarch.rpm: 头V4 DSA/SHA1 Signature, 密钥 ID d50582e6: NOKEY</div><div class="line">准备中...                                                            (<span class="comment">################################# [100%]</span></div><div class="line">正在升级/安装...</div><div class="line">   1:jenkins-2.78-1.1                                                 <span class="comment">################################# [100%]</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另外也可以下载war包，只需要运行命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># java -jar jenkins.war</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;jenkins就启动成功了！war包自带jetty服务器，剩下的工作全部在浏览器中进行。</p>
<h2 id="防火墙设置"><a href="#防火墙设置" class="headerlink" title="防火墙设置"></a>防火墙设置</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># systemctl stop firewalld</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">firewall-cmd –zone=public –add-port=8080/tcp –permanent </div><div class="line">firewall-cmd –zone=public –add-service=http –permanent </div><div class="line">firewall-cmd –reload</div></pre></td></tr></table></figure>
<h2 id="启动jenkins服务"><a href="#启动jenkins服务" class="headerlink" title="启动jenkins服务"></a>启动jenkins服务</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动jenkins服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># /etc/init.d/jenkins start</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动失败</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># /etc/init.d/jenkins start</span></div><div class="line">Starting jenkins (via systemctl):  Job <span class="keyword">for</span> jenkins.service failed because the control process exited with error code. See <span class="string">"systemctl status jenkins.service"</span> and <span class="string">"journalctl -xe"</span> <span class="keyword">for</span> details.</div><div class="line">                                                           [失败]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;出错原因是jdk未安装或jenkins配置文件中没有找到jdk的路径</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决办法是安装jdk，修改jenkins启动脚本,配置jdk位置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># vim /etc/init.d/jenkins</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/usr/<span class="built_in">local</span>/jdk1.8.0_111/bin/java</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/02.png?raw=true" alt="02"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果错误提示</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># /etc/init.d/jenkins start</span></div><div class="line">Starting jenkins (via systemctl):  Warning: jenkins.service changed on disk. Run <span class="string">'systemctl daemon-reload'</span> to reload units.</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用命令解决</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># systemctl daemon-reload</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再次启动jenkins</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># /etc/init.d/jenkins restart</span></div><div class="line">Restarting jenkins (via systemctl):                        [  确定  ]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看是否启动</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># netstat -natpu|grep 8080</span></div><div class="line">tcp6       0      0 :::8080                 :::*                    LISTEN      5041/java</div></pre></td></tr></table></figure>
<h2 id="访问jenkins"><a href="#访问jenkins" class="headerlink" title="访问jenkins"></a>访问jenkins</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在浏览器中输入 <a href="http://ip:8080" target="_blank" rel="external">http://ip:8080</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果安装完成并成功启动会有下图</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/03.png?raw=true" alt="03"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第一次启动jenkins时，出于安全考虑，jenkins会自动生成一个随机的密码。查看/var/lib/jenkins/secrets/initialAdminPassword这个文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cat /var/lib/jenkins/secrets/initialAdminPassword</span></div><div class="line">2bdb5321eac84842b6e37a08758d3654</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将密码复制出来，粘贴，继续进入配置页面</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/04.png?raw=true" alt="04"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里可以根据自己需求选择推荐插件或者自己选择插件安装。如果执行默认的安装，jenkins就自动配置好Maven、git等常用插件。</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/05.png?raw=true" alt="05"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后，创建一个admin用户，完成安装。</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/06.png?raw=true" alt="06"></p>
<p><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/07.png?raw=true" alt="07"></p>
<p><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/08.png?raw=true" alt="08"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用管理员账号登录Jenkins后，第一次使用前，需要在“系统管理”-&gt;“Global Tool Configuration”-&gt;“Maven”中新增一个Maven，直接输入一个名字，选中“自动安装”，Jenkins会自动下载并安装Maven</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/09.png?raw=true" alt="09"></p>
<p><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/10.png?raw=true" alt="10"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果不成功就在“管理插件”里边选择“可选插件”，安装<code>Maven Integration plugin</code>。</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/11.png?raw=true" alt="11"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后，在Jenkins首页选择“新建”，输入名字，选择“构建一个maven项目”</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/12.png?raw=true" alt="12"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在配置页中，源码管理选择Git，填入地址</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/13.png?raw=true" alt="13"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;默认使用master分支。如果需要口令，在Credentials中添加用户名/口令，或者使用SSH Key。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;构建触发器指定了触发一次构建的条件。推荐使用最简单的配置“Poll SCM”，它的意思是，定时检查版本库，发现有新的提交就触发构建。这种方式对git、SVN等所有版本管理系统都是通用的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在日程表中填入</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">* * * * *</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/14.png?raw=true" alt="14"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;表示每分钟检查一次。如果觉得太频繁，可以该成“每3分钟检查一次”</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">*/3 * * * *</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在“Build”中，默认的Root POM是<code>pom.xml</code>。如果<code>pom.xml</code>不在根目录下，就填入子目录，例如：<code>wxapi/pom.xml</code>。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在Goals and options中，填入需要执行的mvn命令：<code>clean package</code>，Jenkins将执行如下命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mvn clean package</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;特殊参数也在这里填写，如<code>-DskipTests=true clean package</code>。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存后，就可以执行自动化构建了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;点击一个构建任务，可以在Console Output中看到控制台详细输出，便于出错排查</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/15.png?raw=true" alt="15"></p>
<h2 id="如何部署"><a href="#如何部署" class="headerlink" title="如何部署"></a>如何部署</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果要部署构建好的war包，可以在Post Steps中填上shell命令，直接用脚本部署。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另一种方式是创建另外一个构建项目，手动触发部署。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;无论用哪种方式，都是为了确保编译、部署是通过CI服务器完成的，而不是某台开发机器。</p>
<h2 id="如何创建Linux服务"><a href="#如何创建Linux服务" class="headerlink" title="如何创建Linux服务"></a>如何创建Linux服务</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有了Jenkins，就可以在内网或者租用一台EC2服务器来搭建CI环境。因为不想每次登录到Linux去启动Jenkins，也不想写脚本来启动服务。推荐安装JDK后，配合supervisor，把Jenkins直接变成一个服务。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以在Linux上创建一个<code>ci</code>用户，然后，用supervisor启动并指定9001端口：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># /etc/supervisor/conf.d/ci.conf</span></div><div class="line"></div><div class="line">[program:ci]</div><div class="line"><span class="built_in">command</span>=java -jar /home/ci/jenkins.war --httpPort=9001</div><div class="line">user=ci</div><div class="line">autostart=<span class="literal">true</span></div><div class="line">autorestart=<span class="literal">true</span></div><div class="line">startsecs=30</div><div class="line">startretries=5</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Jenkins默认在当前用户的主目录下创建<code>.jenkins</code>目录，所有的配置文件、数据库都存放在里面，只需要备份这个目录就备份了整个CI配置。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样，一个CI环境就搭建完毕。</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Jenkins/" rel="tag"># Jenkins</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/19/1. Linux 基础/73.利用iptables防止syn flood攻击/" rel="next" title="利用iptables防止syn flood攻击">
                <i class="fa fa-chevron-left"></i> 利用iptables防止syn flood攻击
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/19/jenkins/1.jenkins是什么？/" rel="prev" title="jenkins是什么？">
                jenkins是什么？ <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#安装JDK"><span class="nav-number">1.</span> <span class="nav-text">安装JDK</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#下载安装jenkins"><span class="nav-number">2.</span> <span class="nav-text">下载安装jenkins</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#防火墙设置"><span class="nav-number">3.</span> <span class="nav-text">防火墙设置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#启动jenkins服务"><span class="nav-number">4.</span> <span class="nav-text">启动jenkins服务</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#访问jenkins"><span class="nav-number">5.</span> <span class="nav-text">访问jenkins</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#如何部署"><span class="nav-number">6.</span> <span class="nav-text">如何部署</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#如何创建Linux服务"><span class="nav-number">7.</span> <span class="nav-text">如何创建Linux服务</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
